package cn.lkk.samples.lang.classes.PredefinedClasses;

import java.text.DateFormatSymbols;
import java.util.*;

/**
 * 一个示例，用于打印本月日历，并将今天进行标注。用于演示日期相关的类的使用。
 */
public class CurrentCalendarPrint {
    public static void main(String[] args) {


        GregorianCalendar day = new GregorianCalendar();
        int month = day.get(Calendar.MONTH);//获得本月
        int today = day.get(Calendar.DAY_OF_MONTH);//获得今日

        day.set(Calendar.DAY_OF_MONTH, 1);//将日历设置到第一天
        int firstDayOfWeek = day.getFirstDayOfWeek();

        // 计算缩进
        int indent = 0;
        while (day.get(Calendar.DAY_OF_WEEK) != firstDayOfWeek) {
            indent++;
            day.add(Calendar.DAY_OF_MONTH, -1);
        }

        //打印表头
        String[] weekdays = new DateFormatSymbols(Locale.US).getShortWeekdays();
        do {
            System.out.printf("%4s", weekdays[day.get(Calendar.DAY_OF_WEEK)]);
            day.add(Calendar.DAY_OF_MONTH, 1);
        } while (day.get(Calendar.DAY_OF_WEEK) != firstDayOfWeek);
        System.out.println();

        //打印缩进
        while (indent > 0) {
            System.out.print("    ");
            indent--;
        }

        //依次打印每一天
        day.set(Calendar.DAY_OF_MONTH, 1);//将日历设置到第一天
        while (month == day.get(Calendar.MONTH)) {

            //换行
            if (day.get(Calendar.DAY_OF_WEEK) == firstDayOfWeek)
                System.out.println();

            //打印当前这一天
            int dayOfMoth = day.get(Calendar.DAY_OF_MONTH);
            System.out.printf("%3s", dayOfMoth);

            //在今天后面打印一个星号
            if (dayOfMoth == today)
                System.out.print("*");
            else
                System.out.print(" ");

            day.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
}
